package pl.gpms.server.persistance.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import pl.gpms.client.dto.EstimateViewDTO;
import pl.gpms.server.persistance.map.EstimateView;
import pl.gpms.server.persistance.tools.HibernateUtil;

public class EstimateViewDao implements Serializable {

	private static final long serialVersionUID = 1L;
	
	public EstimateViewDTO createEstimateViewDTO(EstimateView estimateView) {
		EstimateDao estimateDao = new EstimateDao();
		return new EstimateViewDTO( estimateView.getAge(),
									estimateView.getAttrib(),
									estimateView.getResult(),
									estimateView.getSymptoms(),
									estimateDao.createEstimateDTO(estimateView.getEstimate()));
	}
	
	@SuppressWarnings("unchecked")
	public List<EstimateViewDTO> getEstimateForObject(Long id) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		List<EstimateView> estimateView = 
				new ArrayList<EstimateView>(session.createCriteria(EstimateView.class).add(Restrictions.eq("objectId", id)).list()
				);
		List<EstimateViewDTO> estimateViewDTO = 
				new ArrayList<EstimateViewDTO>(estimateView != null ? estimateView.size() : 0);
		for(EstimateView estView : estimateView) {
			estimateViewDTO.add(createEstimateViewDTO(estView));
		}
		session.getTransaction().commit();
		session.close();
		return estimateViewDTO;
	}
}
